package com.tianhui.consignortwofei.mvp.ui.activity.hetong.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.fgs.common.utils.CommonUtil;
import com.fgs.common.utils.LogUtil;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.mvp.model.hetong.ProductInfo;


import org.yczbj.ycrefreshviewlib.adapter.RecyclerArrayAdapter;
import org.yczbj.ycrefreshviewlib.holder.BaseViewHolder;

import java.math.BigDecimal;

/**
 * @author fgs
 * @date 2021/5/20
 * @description
 **/
public class ProductAdapter extends RecyclerArrayAdapter<ProductInfo> {

    public ProductAdapter(Context context) {
        super(context);
    }


    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new ProductInfoViewHolder(parent, R.layout.item_product);
    }

    public class ProductInfoViewHolder extends BaseViewHolder<ProductInfo> {

        private TextView serialNumberTextView;

        private EditText productNameEditText;
        private EditText specificationsEditText;
        private EditText factoryEditText;

        private EditText measurementUnitEditText;
        private EditText numberEditText;
        private EditText unitPriceEditText;

        private EditText salesVolumeEditText;
        private EditText taxValueEditText;
        private EditText totalEditText;

        private EditText remarkEditText;


        private NumberTextWatcher numberTextWatcher;
        private PriceTextWatcher priceTextWatcher;
        private NameTextWatcher nameTextWatcher;
        private TypeTextWatcher typeTextWatcher;
        private FactoryTextWatcher factoryTextWatcher;
        private UnitTextWatcher unitTextWatcher;
        private RemarkTextWatcher remarkTextWatcher;


        public ProductInfoViewHolder(ViewGroup parent, int res) {
            super(parent, res);
            serialNumberTextView = getView(R.id.item_product_serialNumberTextView);
            productNameEditText = getView(R.id.item_product_productNameEditText);
            specificationsEditText = getView(R.id.item_product_specificationsEditText);
            factoryEditText = getView(R.id.item_product_factoryEditText);
            measurementUnitEditText = getView(R.id.item_product_measurementUnitEditText);
            numberEditText = getView(R.id.item_product_numberEditText);
            unitPriceEditText = getView(R.id.item_product_unitPriceEditText);
            salesVolumeEditText = getView(R.id.item_product_salesVolumeEditText);
            taxValueEditText = getView(R.id.item_product_taxValueEditText);
            totalEditText = getView(R.id.item_product_totalEditText);
            remarkEditText = getView(R.id.item_product_remarkEditText);

            numberTextWatcher = new NumberTextWatcher();
            priceTextWatcher = new PriceTextWatcher();

            nameTextWatcher = new NameTextWatcher();
            typeTextWatcher = new TypeTextWatcher();
            factoryTextWatcher = new FactoryTextWatcher();
            unitTextWatcher = new UnitTextWatcher();
            remarkTextWatcher = new RemarkTextWatcher();

            addOnClickListener(R.id.item_product_deleteButton);
        }

        @Override
        public void setData(ProductInfo data) {
            super.setData(data);

            if (numberEditText.getTag() instanceof TextWatcher) {
                numberEditText.removeTextChangedListener(((TextWatcher) numberEditText.getTag()));
            }
            if (unitPriceEditText.getTag() instanceof TextWatcher) {
                unitPriceEditText.removeTextChangedListener(((TextWatcher) unitPriceEditText.getTag()));
            }

            if (productNameEditText.getTag() instanceof TextWatcher) {
                productNameEditText.removeTextChangedListener(((TextWatcher) productNameEditText.getTag()));
            }

            if (specificationsEditText.getTag() instanceof TextWatcher) {
                specificationsEditText.removeTextChangedListener(((TextWatcher) specificationsEditText.getTag()));
            }

            if (factoryEditText.getTag() instanceof TextWatcher) {
                factoryEditText.removeTextChangedListener(((TextWatcher) factoryEditText.getTag()));
            }

            if (measurementUnitEditText.getTag() instanceof TextWatcher) {
                measurementUnitEditText.removeTextChangedListener(((TextWatcher) measurementUnitEditText.getTag()));
            }
            if (remarkEditText.getTag() instanceof TextWatcher) {
                remarkEditText.removeTextChangedListener(((TextWatcher) remarkEditText.getTag()));
            }

            serialNumberTextView.setText("产品信息");

            productNameEditText.setText(data.name);
            specificationsEditText.setText(data.type);
            factoryEditText.setText(data.factory);

            measurementUnitEditText.setText(data.unit);
            numberEditText.setText(data.number);
            if (!TextUtils.isEmpty(data.number)) {
                numberEditText.setSelection(data.number.length());
            }
            unitPriceEditText.setText(data.price);
            if (!TextUtils.isEmpty(data.price)) {
                unitPriceEditText.setSelection(data.price.length());
            }

            salesVolumeEditText.setText(data.sales);
            taxValueEditText.setText(data.vat);
            totalEditText.setText(data.total);

            remarkEditText.setText(data.remark);

            numberTextWatcher.buildTextWatcher(data, getDataPosition());
            numberEditText.addTextChangedListener(numberTextWatcher);
            numberEditText.setTag(numberTextWatcher);

            priceTextWatcher.buildTextWatcher(data, getDataPosition());
            unitPriceEditText.addTextChangedListener(priceTextWatcher);
            unitPriceEditText.setTag(priceTextWatcher);

            nameTextWatcher.buildTextWatcher(data, getDataPosition());
            productNameEditText.addTextChangedListener(nameTextWatcher);
            productNameEditText.setTag(nameTextWatcher);

            typeTextWatcher.buildTextWatcher(data, getDataPosition());
            specificationsEditText.addTextChangedListener(typeTextWatcher);
            specificationsEditText.setTag(typeTextWatcher);

            factoryTextWatcher.buildTextWatcher(data, getDataPosition());
            factoryEditText.addTextChangedListener(factoryTextWatcher);
            factoryEditText.setTag(factoryTextWatcher);

            unitTextWatcher.buildTextWatcher(data, getDataPosition());
            measurementUnitEditText.addTextChangedListener(unitTextWatcher);
            measurementUnitEditText.setTag(unitTextWatcher);

            remarkTextWatcher.buildTextWatcher(data, getDataPosition());
            remarkEditText.addTextChangedListener(remarkTextWatcher);
            remarkEditText.setTag(remarkTextWatcher);
        }


    }

    private void calculate(ProductInfo productInfo) {
//        if (!TextUtils.isEmpty(productInfo.number) && !TextUtils.isEmpty(productInfo.price)) {
        if (TextUtils.isEmpty(productInfo.number)) {
            productInfo.number = "0";
        }
        if (TextUtils.isEmpty(productInfo.price)) {
            productInfo.price = "0";
        }
        String total = new BigDecimal(productInfo.number).multiply(new BigDecimal(productInfo.price)).toString();
        productInfo.total = CommonUtil.keepThreeDecimal(total);
        LogUtil.print("total", total);

        String sales = new BigDecimal(total).divide(new BigDecimal("1.13"), 2, BigDecimal.ROUND_HALF_UP).toString();

        productInfo.sales = sales;
        LogUtil.print("sales", sales);
        String vat = new BigDecimal(total).subtract(new BigDecimal(sales)).toString();
        productInfo.vat = CommonUtil.keepThreeDecimal(vat);
        LogUtil.print("vat", vat);

//        }
    }

    private class NumberTextWatcher implements TextWatcher {
        private ProductInfo productInfo;
        private int position;

        public void buildTextWatcher(ProductInfo productInfo, int position) {
            this.productInfo = productInfo;
            this.position = position;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (!TextUtils.isEmpty(s.toString())) {
                productInfo.number = s.toString();
                calculate(productInfo);
                notifyItemChanged(position);
            }
        }
    }

    private class PriceTextWatcher implements TextWatcher {
        private ProductInfo productInfo;
        private int position;

        public void buildTextWatcher(ProductInfo productInfo, int position) {
            this.productInfo = productInfo;
            this.position = position;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (!TextUtils.isEmpty(s.toString())) {

                productInfo.price = s.toString();
                calculate(productInfo);
                notifyItemChanged(position);
            }
        }
    }

    private class NameTextWatcher implements TextWatcher {
        private ProductInfo productInfo;
        private int position;

        public void buildTextWatcher(ProductInfo productInfo, int position) {
            this.productInfo = productInfo;
            this.position = position;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (!TextUtils.isEmpty(s.toString())) {
                productInfo.name = s.toString();
            }
        }
    }

    private class TypeTextWatcher implements TextWatcher {
        private ProductInfo productInfo;
        private int position;

        public void buildTextWatcher(ProductInfo productInfo, int position) {
            this.productInfo = productInfo;
            this.position = position;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (!TextUtils.isEmpty(s.toString())) {
                productInfo.type = s.toString();
            }
        }
    }

    private class FactoryTextWatcher implements TextWatcher {
        private ProductInfo productInfo;
        private int position;

        public void buildTextWatcher(ProductInfo productInfo, int position) {
            this.productInfo = productInfo;
            this.position = position;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (!TextUtils.isEmpty(s.toString())) {
                productInfo.factory = s.toString();
            }
        }
    }

    private class UnitTextWatcher implements TextWatcher {
        private ProductInfo productInfo;
        private int position;

        public void buildTextWatcher(ProductInfo productInfo, int position) {
            this.productInfo = productInfo;
            this.position = position;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (!TextUtils.isEmpty(s.toString())) {
                productInfo.unit = s.toString();
            }
        }
    }

    private class RemarkTextWatcher implements TextWatcher {
        private ProductInfo productInfo;
        private int position;

        public void buildTextWatcher(ProductInfo productInfo, int position) {
            this.productInfo = productInfo;
            this.position = position;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (!TextUtils.isEmpty(s.toString())) {
                productInfo.remark = s.toString();
            }
        }
    }
}
